package com.jlz.beans.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jlz.managers.def.Columns;
import com.julewa.Configuration;
import com.julewa.db.DB;
import com.julewa.db.DB.AUTOTRIM;
import com.julewa.db.Entity;
import com.julewa.db.mongo.MongoDB;

/*
 * 资产类型列表
 * 用以管理资产的名称
 * 重要的是管理资产的网页模板
 * 
 * 每一类资产,都有相应的模板
 * 直接与code关联
 * 这个模板将有可能缺省生成html/wap等多个版本
 * 
 * 每一类资产也都有相应的描述属性
 * 还有合同属性
 * 
 * TODO 先不考虑复杂的有效性验证规则如何做
 * 基本的思路可以考虑用JS来处理,
 * 如此前端和后端都可通用
 * 
 */
@DB.USECACHE
@Component @Scope(Entity.SCOPE)
public class CategoryBean extends Entity<Long>{
	//
	private @DB.COLUMN(min=2, max=32) @AUTOTRIM String code = "";
	private @DB.COLUMN(min=2, max=64) @AUTOTRIM String label = "";
	private @DB.COLUMN(min=0, max=32) int type = 0;
	private @DB.COLUMN(max=128) @AUTOTRIM String description = "";
	//
	private @DB.COLUMN long assetColumnRoot = -1;
	private @DB.COLUMN long desireColumnRoot = -1;
	private @DB.COLUMN long contractColumnRoot = -1;
	//

	private @Autowired MongoDB mongoDB;
	private @Autowired Configuration config;
	private @DB.DEPENDON("type") String typeName = null; 
	private @Autowired Columns columns;
	
	//
	private CategoryTable asset = null; 
	private CategoryTable desire = null; 
	private CategoryTable contract = null; 
	
	public CategoryTable getAssetTable(){
		if(asset != null)return asset;
		if(this.assetColumnRoot < 0){
			return null;
		}
		synchronized(this){
			asset = new CategoryTable(mongoDB, this, columns, assetColumnRoot);
		}
		return asset;
	}

	public CategoryTable getDesireTable(){
		if(desire != null)return desire;
		if(this.desireColumnRoot < 0)return null;
		synchronized(this){
			desire = new CategoryTable(mongoDB, this, columns, desireColumnRoot);
		}
		return desire;
	}
	
	public CategoryTable getContractTable(){
		if(contract != null)return contract;
		if(this.contractColumnRoot < 0)return null;
		synchronized(this){
			contract = new CategoryTable(mongoDB, this, columns, contractColumnRoot);
		}
		return contract;
	}

	//
	public String getTypeName(){
		if(typeName == null){ 
			typeName = config.getProperty("category.type." + type); 
		}
		return typeName;
	}
	
	//
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	//
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public long getAssetColumnRoot() {
		return assetColumnRoot;
	}
	public void setAssetColumnRoot(long assetColumnRoot) {
		this.assetColumnRoot = assetColumnRoot;
	}
	public long getDesireColumnRoot() {
		return desireColumnRoot;
	}
	public void setDesireColumnRoot(long desireColumnRoot) {
		this.desireColumnRoot = desireColumnRoot;
	}
	public long getContractColumnRoot() {
		return contractColumnRoot;
	}
	public void setContractColumnRoot(long contractColumnRoot) {
		this.contractColumnRoot = contractColumnRoot;
	}
	
	
}
